package util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;


/**
 * MD5加密工具
 *
 * @author wyl
 */
public final class MD5Helper {
    //盐，用于混交md5
    private static final String SLAT = "&%5123***&&%%$$#@";


    /**
     * md5加密
     *
     * @param dataStr 需要加密的字符串
     * @return md5加密后的字符串
     */
    public static String encrypt(String dataStr) throws Exception {
        try {
            dataStr = dataStr + SLAT;
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(dataStr.getBytes(StandardCharsets.UTF_8));
            byte[] s = m.digest();
            StringBuilder result = new StringBuilder();
            for (byte b : s) {
                result.append(Integer.toHexString((0x000000FF & b) | 0xFFFFFF00).substring(6));
            }
            return result.toString();
        } catch (Exception e) {
            System.out.println("md5加密失败");
            throw e;
        }

    }
}
